package com.erp.shop.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.shop.domain.bo.ErpSellerStoreEmpowerExtendBo;
import com.erp.shop.domain.vo.ErpSellerStoreEmpowerExtendVo;
import com.erp.shop.mapper.ErpSellerStoreEmpowerExtendMapper;
import com.erp.shop.mapper.ErpSellerStoreEmpowerMapper;
import com.erp.shop.service.IErpSellerStoreEmpowerExtendService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.oldUtils.StringUtils;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import com.ruoyi.shop.api.domain.ErpSellerStoreEmpowerExtend;
import com.sun.org.apache.regexp.internal.RE;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 卖家店铺Ae授权信息Service业务层处理
 *
 * @author erp
 * @date 2023-04-20
 */
@RequiredArgsConstructor
@Service
public class ErpSellerStoreEmpowerExtendServiceImpl implements IErpSellerStoreEmpowerExtendService {

    private final ErpSellerStoreEmpowerExtendMapper baseMapper;
    private final ErpSellerStoreEmpowerMapper iErpSellerStoreEmpowerService;

    /**
     * 查询卖家店铺Ae授权信息
     */
    @Override
    public ErpSellerStoreEmpowerExtendVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询卖家店铺Ae授权信息列表
     */
    @Override
    public TableDataInfo<ErpSellerStoreEmpowerExtendVo> queryPageList(ErpSellerStoreEmpowerExtendBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpSellerStoreEmpowerExtend> lqw = buildQueryWrapper(bo);
        Page<ErpSellerStoreEmpowerExtendVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询卖家店铺Ae授权信息列表
     */
    @Override
    public List<ErpSellerStoreEmpowerExtendVo> queryList(ErpSellerStoreEmpowerExtendBo bo) {
        LambdaQueryWrapper<ErpSellerStoreEmpowerExtend> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ErpSellerStoreEmpowerExtend> buildQueryWrapper(ErpSellerStoreEmpowerExtendBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpSellerStoreEmpowerExtend> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExpiresIn() != null, ErpSellerStoreEmpowerExtend::getExpiresIn, bo.getExpiresIn());
        lqw.eq(bo.getSellerId() != null, ErpSellerStoreEmpowerExtend::getSellerId, bo.getSellerId());
        lqw.eq(StringUtils.isNotBlank(bo.getUserId()), ErpSellerStoreEmpowerExtend::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getUserNick()), ErpSellerStoreEmpowerExtend::getUserNick, bo.getUserNick());
        lqw.eq(StringUtils.isNotBlank(bo.getHavanaId()), ErpSellerStoreEmpowerExtend::getHavanaId, bo.getHavanaId());
        lqw.eq(StringUtils.isNotBlank(bo.getAccountPlatform()), ErpSellerStoreEmpowerExtend::getAccountPlatform, bo.getAccountPlatform());
        lqw.eq(StringUtils.isNotBlank(bo.getAccessToken()), ErpSellerStoreEmpowerExtend::getAccessToken, bo.getAccessToken());
        lqw.eq(StringUtils.isNotBlank(bo.getAccount()), ErpSellerStoreEmpowerExtend::getAccount, bo.getAccount());
        lqw.eq(StringUtils.isNotBlank(bo.getRefreshToken()), ErpSellerStoreEmpowerExtend::getRefreshToken, bo.getRefreshToken());
        lqw.eq(bo.getRefreshExpiresIn() != null, ErpSellerStoreEmpowerExtend::getRefreshExpiresIn, bo.getRefreshExpiresIn());
        lqw.eq(bo.getExpireTime() != null, ErpSellerStoreEmpowerExtend::getExpireTime, bo.getExpireTime());
        lqw.eq(bo.getRefreshTokenValidTime() != null, ErpSellerStoreEmpowerExtend::getRefreshTokenValidTime, bo.getRefreshTokenValidTime());
        lqw.eq(StringUtils.isNotBlank(bo.getSp()), ErpSellerStoreEmpowerExtend::getSp, bo.getSp());
        lqw.eq(StringUtils.isNotBlank(bo.getLocale()), ErpSellerStoreEmpowerExtend::getLocale, bo.getLocale());
        lqw.eq(StringUtils.isNotBlank(bo.getAccountId()), ErpSellerStoreEmpowerExtend::getAccountId, bo.getAccountId());
        lqw.eq(bo.getType() != null, ErpSellerStoreEmpowerExtend::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getRevision()), ErpSellerStoreEmpowerExtend::getRevision, bo.getRevision());
        return lqw;
    }

    /**
     * 新增卖家店铺Ae授权信息
     */
    @Override
    public Boolean insertByBo(ErpSellerStoreEmpowerExtendBo bo) {
        ErpSellerStoreEmpowerExtend add = BeanUtil.toBean(bo, ErpSellerStoreEmpowerExtend.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改卖家店铺Ae授权信息
     */
    @Override
    public Boolean updateByBo(ErpSellerStoreEmpowerExtendBo bo) {
        ErpSellerStoreEmpowerExtend update = BeanUtil.toBean(bo, ErpSellerStoreEmpowerExtend.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ErpSellerStoreEmpowerExtend entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除卖家店铺Ae授权信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public ErpSellerStoreEmpowerExtend selectByUserNick(String userNick) {
        return baseMapper.selectOne(new LambdaQueryWrapper<ErpSellerStoreEmpowerExtend>()
                .eq(ErpSellerStoreEmpowerExtend::getUserNick, userNick)
                .eq(ErpSellerStoreEmpowerExtend::getType, 1)
        );
    }

    @Override
    public ErpSellerStoreEmpowerExtend queryBySellerId(Long storeId) {
        return baseMapper.selectOne(new LambdaQueryWrapper<ErpSellerStoreEmpowerExtend>()
                .eq(ErpSellerStoreEmpowerExtend::getSellerId, storeId)
                .eq(ErpSellerStoreEmpowerExtend::getType, 1));
    }

    @Override
    public Integer failureToken(Long storeId) {
        ErpSellerStoreEmpowerExtend erpSellerStoreEmpowerExtend = new ErpSellerStoreEmpowerExtend();
        erpSellerStoreEmpowerExtend.setType(0);
        return baseMapper.update(
                erpSellerStoreEmpowerExtend,
                new LambdaQueryWrapper<ErpSellerStoreEmpowerExtend>()
                        .eq(ErpSellerStoreEmpowerExtend::getSellerId, storeId)
        );
    }

    @Override
    public Integer updateById(ErpSellerStoreEmpowerExtend empowerExtend) {
        return baseMapper.updateById(empowerExtend);
    }

    @Override
    public Integer insertEmpowerRxtend(ErpSellerStoreEmpowerExtend empowerExtend) {
        return baseMapper.insert(empowerExtend);
    }

    @Override
    public R<Object> getSellerStoreExtendInfo(ErpSellerStoreEmpowerExtend erpSellerStoreEmpowerExtendSel) {
        ErpSellerStoreEmpowerExtendBo bo = BeanUtil.copyProperties(erpSellerStoreEmpowerExtendSel, ErpSellerStoreEmpowerExtendBo.class);
        ErpSellerStoreEmpowerExtend erpSellerStoreEmpowerExtend = baseMapper.selectOne(buildQueryWrapper(bo));
        if (Objects.isNull(erpSellerStoreEmpowerExtend)){
            return R.fail("获取信息为空");
        }else {
            return R.ok(erpSellerStoreEmpowerExtend);
        }
    }

    @Override
    public R<Object> getSellerStoreExtendInfoByStoreId(String storeId) {
        ErpSellerStoreEmpowerExtend erpSellerStoreEmpowerExtend = baseMapper.selectOne(
                new LambdaQueryWrapper<ErpSellerStoreEmpowerExtend>().eq(ErpSellerStoreEmpowerExtend::getSellerId,storeId)
        );
        if (Objects.isNull(erpSellerStoreEmpowerExtend)){
            return R.fail("获取信息为空");
        }else {
            return R.ok(erpSellerStoreEmpowerExtend);
        }
    }

}
